package com.pufa.demo.dao;

import org.dom4j.Document;
import org.springframework.stereotype.Repository;

import com.pufa.util.Dom4jUtil;

/**
 * 
 * @author Menlin
 *
 */
@Repository("demoDAO")
public class DemoDAO implements IDemoDAO {

	@Override
	public boolean save(String pingzh) {
		try {
			Document doc = DataSource.getDataSource();
			Dom4jUtil.appendChile(pingzh, doc.getRootElement());
			Dom4jUtil.writeDoctoXML(DataSource.XML_PATH, doc);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public String queryByPingzh(String pingzh) {
		try {
			Document doc = DataSource.getDataSource();
			return Dom4jUtil.getNodeByName(pingzh, doc) != null 
					? Dom4jUtil.getNodeByName(pingzh, doc).getName() 
							: null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
